package com.ikangtai.shecare.curve;


import android.content.Context;
import android.webkit.JavascriptInterface;

import com.ikangtai.shecare.common.App;
import com.ikangtai.shecare.common.al.Algorithm;
import com.ikangtai.shecare.common.al.DayUnitDSOutput;
import com.ikangtai.shecare.common.eventBusMsg.CurveMsg;
import com.ikangtai.shecare.common.util.AppLog;
import com.ikangtai.shecare.common.util.DateUtil;
import com.ikangtai.shecare.record.bean.UserRecordData;
import com.ikangtai.shecare.record.bean.UserTemperatureInfo;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.simple.eventbus.EventBus;

import java.util.List;
import java.util.Locale;

public final class CustomerData {
    private Context context;
    private String userName;

    public CustomerData(Context context, String userName) {
        this.context = context;
        this.userName = userName;
        EventBus.getDefault().register(this);
    }

    @JavascriptInterface
    public String getCustomerDataJSONStr() throws NullArrayException {
        Algorithm algorithm = new Algorithm(context);
        DayUnitDSOutput[] dayOutput = algorithm.getALDayOutputFromSqlite(userName);

        if (dayOutput == null)
            throw new NullArrayException("DayUnitDSOutput[] is null in getCustomerDataJSONStr()!");
        else
            return getComposedCustomerDataJSONStr(dayOutput);
    }

    public class NullArrayException extends Exception {
        public NullArrayException(String detailMessage) {
            super(detailMessage);
        }
    }

    private String getComposedCustomerDataJSONStr(DayUnitDSOutput[] dayOutput) {

        JSONObject customerDataJSONobj = new JSONObject();

        try {
            customerDataJSONobj.put("userName", userName);

            String lang = Locale.getDefault().getLanguage();

            if(lang.contains("zh") || lang.contains("cn"))
                lang = "chinese";
            else
                lang = "english";

            //centigrade or fahrenheit
            String unit = "centigrade";

            AppLog.i("your current language is " + Locale.getDefault().getLanguage() + ", lang = " + lang);

            customerDataJSONobj.put("language", lang);
            customerDataJSONobj.put("unit", unit);

            customerDataJSONobj.put("state", App.status);

            JSONArray customerDataJSONArray = new JSONArray();

            String formatDateStr;
            String periodStr;
            String temperatureStr;
            String heartStr;
            String moonStr;
            String drinkStr;
            String medicineStr;
            String planeStr;
            int mucus;

            long startTime = dayOutput[0].date;
            long endTime = dayOutput[dayOutput.length - 1].date;
            List<UserRecordData> userRecordDataList = UserRecordData.getUserRecordDataList(context, App.userName, startTime, endTime);
            List<UserTemperatureInfo> BBTList = UserTemperatureInfo.getBBTList(context, App.userName, startTime, endTime);

            int userRecordDataListLen = userRecordDataList.size();

            for(int i = 0; i < userRecordDataListLen; i++){
                JSONObject customerDateDataJSONObj = new JSONObject();

                formatDateStr = DateUtil.getDateStr(dayOutput[i].date);
                periodStr = dayOutput[i].getPeriod(App.status);
                temperatureStr = getBBTStr(BBTList.get(i).getTemperature());
                AppLog.i(i + ". formatDateStr = " + formatDateStr + ", temperatureStr = " +  temperatureStr);
                heartStr = getYesNoStr(userRecordDataList.get(i).isCopulationHaveSex());
                moonStr = getYesNoStr(userRecordDataList.get(i).isLostSleep());
                drinkStr = "no"; //新版已删除此字段
                medicineStr = getYesNoStr(userRecordDataList.get(i).isSick());
                planeStr = getYesNoStr(userRecordDataList.get(i).isLongTrip());
                mucus = userRecordDataList.get(i).getMucusStatusValue();

                customerDateDataJSONObj.put("date", formatDateStr);
                customerDateDataJSONObj.put("period", periodStr);
                customerDateDataJSONObj.put("temperature", temperatureStr);
                customerDateDataJSONObj.put("heart", heartStr);
                customerDateDataJSONObj.put("moon", moonStr);
                customerDateDataJSONObj.put("drink", drinkStr);
                customerDateDataJSONObj.put("medicine", medicineStr);
                customerDateDataJSONObj.put("plane", planeStr);
                customerDateDataJSONObj.put("mucus", mucus);

                customerDataJSONArray.put(customerDateDataJSONObj);
            }
            customerDataJSONobj.put("customerDataJSONArray", customerDataJSONArray);
        }catch (JSONException e){
            AppLog.i("JSONException!");
            e.printStackTrace();
        }

        AppLog.i("hxiao customerDataJSONobj.toString() = " + customerDataJSONobj.toString());
        return customerDataJSONobj.toString();
    }

    private String getYesNoStr(boolean tf) {
        return tf ? "yes" : "no";
    }

    private String getBBTStr(double BBT) {
        return BBT == 0 ? "-" : String.valueOf(BBT);
    }

    @JavascriptInterface
    public void printJS(String str){
        AppLog.i("JS str = " + str);
    }

    @JavascriptInterface
    public void sendCurveMsg(){
        EventBus.getDefault().post(new CurveMsg());
    }
}
